<?
    require_once("Net/IPv4.php");
    
    class Principio_Library_ControllerAction extends Zend_Controller_Action implements Principio_Library_PostValidationInterface
    {
        protected $post_data;
        protected $is_post;
        protected $is_apply;
        
        public function init()
        {
            if ($this->_request->isPost())
            {
                $this->post_data = $this->_request->getPost();
                $this->is_post = true;
                $this->is_apply = ( isset($this->post_data['apply']) && (intval($this->post_data['apply']) == 1));
                
            }
            else
            {
                $this->post_data = null;
                $this->is_post = false;
                $this->is_apply = false;
            };
            
            parent::init();
        }

        public function checkPostItem($item_name)
        {
            return Principio_Library_Common::checkValidity(isset($this->post_data[$item_name]), "Script problem: there is no [".$item_name."] parameter in the request");
        }

        public function checkPostItemString($item_name, $item_name_public)
        {
            if ($this->checkPostItem($item_name))
            {
                if (Principio_Library_Common::checkValidity($this->post_data[$item_name] != "", $item_name_public." can't be empty"))
                    $this->post_data[$item_name] = $this->post_data[$item_name];
                else
                    $this->post_data[$item_name] = "";
            }
            else
                $this->post_data[$item_name] = "";

            return ($this->post_data[$item_name] != "");
        }

        public function checkPostItemInt($item_name, $item_name_public)
        {
            if ($this->checkPostItem($item_name))
            {
                if (Principio_Library_Common::checkValidity(intval($this->post_data[$item_name]) > 0, $item_name_public." must be greater 0"))
                    $this->post_data[$item_name] = intval($this->post_data[$item_name]);
                else
                    $this->post_data[$item_name] = -1;
            }
            else
                $this->post_data[$item_name] = -1;

            return ($this->post_data[$item_name] != -1);
        }

        public function checkPostItemIP($item_name, $item_name_public)
        {
            if ($this->checkPostItem($item_name))
            {
                if (Principio_Library_Common::checkValidity(Net_IPv4::validateIP($this->post_data[$item_name]), $item_name_public." is not a valid IP"))
                    $this->post_data[$item_name] = $this->post_data[$item_name];
                else
                    $this->post_data[$item_name] = "";
            }
            else
                $this->post_data[$item_name] = "";

            return ($this->post_data[$item_name] != "");
        }
    }
?>
